import axios from 'axios';
import {getToken, removeToken} from './Auth';
import store from '../store';
import {Alert} from 'react-native';
import {Set_Token} from '../store/actions/user';
import {mapDispatchToPropsFactory} from 'react-redux/es/connect/mapDispatchToProps';
import {Loading} from './Loading';
// import {connect} from 'react-redux';


// create an axios instance
const service = axios.create({
    baseURL: 'https://dkapi.toyong.cn', // url = base url + request url
    //baseURL: 'http://172.20.10.3:9000', // url = base url + request url
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 10000, // request timeout
});
service.interceptors.request.use(
    async (config) => {
        //全局拦截
        console.log('全局拦截', store.getState().userReducer.token);
        if (store.getState().userReducer.token) {
            let data = await getToken();
            config.headers['token'] = data;
        }
        return config;
    },
    error => {
        // do something with request error
        console.log(error); // for debug
        return Promise.reject(error);
    },
);

// response interceptor
service.interceptors.response.use(
    /**
     * If you want to get http information such as headers or status
     * Please return  response => response
     */

    /**
     * Determine the request status by custom code
     * Here is just an example
     * You can also judge the status by HTTP Status Code
     */
    response => {
        const res = response.data;
        //console.log(res, 'response.data');
        // if the custom code is not 20000, it is judged as an error.
        if (res.code !== 0) {
            //401令牌过期
            // if (res.code === 500) {
            //
            //     //清空状态
            //     Alert.alert('登陆失败', '账号或密码错误,请重新输入。', [{
            //         text: '确定', onPress: () => {
            //         },
            //     }]);
            // }
            //401令牌过期
            if (res.code === 401) {
                //清空状态
                Alert.alert('安全提醒', '身份验证失败，请重新登陆。', [{
                    text: '确定', onPress: () => {
                        //清空本地的token
                        removeToken();
                        //清除全局状态token
                        let action = Set_Token(null);
                        store.dispatch(action);
                    },
                }]);
            }
            console.log(res, 'response');
            return Promise.reject(new Error(res.msg || '错误：'));
        } else {
            return res;
        }
    },
    error => {
        console.log('err' + error); // for debug
        // Message({
        //   message: error.message,
        //   type: 'error',
        //   duration: 5 * 1000
        // })
        // Loading.hidden()
        return Promise.reject(error);
    },
);

// const mapDispatchToProps = (dispatch) => {
//     return {
//         logout() {
//             let action = Set_Token(null);
//             dispatch(action);
//         },
//     };
// };
export default service;

// export default service;
// const dispatchToProps = (dispatch) => {
//     return {
//         logout() {
//             //清空状态
//             let action = Set_Token(null);
//             dispatch(action);
//         },
//     };
// };
